package com.blogspot.radialmind;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class MultiGraph extends AbstractGraph {
	
	private Map<String,List<List<ITerm>>> data = new TreeMap<String,List<List<ITerm>>>();
	private Map<String,List<ITerm>> averages = new TreeMap<String,List<ITerm>>();
	
	public MultiGraph( String title, List<ITerm> columns ) {
		super( title, columns );
		
		COLUMN_START_INDEX = 3;
	}
	
	public void addData( List<ITerm> terms ) {
		if ( data.containsKey( terms.get( 2 ).toString() )) {
			List<List<ITerm>> list = data.get( terms.get( 2 ).toString() );
			list.add( terms );
		} else {
			List<List<ITerm>> list = new ArrayList<List<ITerm>>();
			list.add( terms );
			data.put( terms.get( 2 ).toString(), list );
		}
	}
	
	public void addAverage( List<ITerm> terms ) {
		if ( ! averages.containsKey( terms.get( 2 ).toString() )) {
			averages.put( terms.get( 2 ).toString(), terms );
		}
	}
	
	public long getStartTimestamp() {
		if ( data.size() > 0 ) {
			List<ITerm> list = data.get( data.keySet().iterator().next() ).get( 0 );
			DateTerm dt = (DateTerm)list.get( 1 );
			return dt.getDate().getTime() / 1000L;
		}
		return 0L;
	}
	public long getEndTimestamp() {
		if ( data.size() > 0 ) {
			Object key = data.keySet().iterator().next();
			List<ITerm> list = data.get( key ).get( data.get( key ).size() - 1 );
			DateTerm dt = (DateTerm)list.get( 1 );
			return dt.getDate().getTime() / 1000L;
		}
		return 0L;		
	}
	
	public String getName() {
		return getTitle();
	}
	public ITerm getData( String key, int row, int col ) {
		return data.get( key ).get( row ).get( col );	
	}
	public int getNumDataRows() {
		Object key = data.keySet().iterator().next();
		return data.get( key ).size();
	}
	public Set<String> getKeys() {
		return data.keySet();
	}
}
